package com.jvxb.manage.livable.controller;

import com.jvxb.manage.livable.entity.SysDict;
import com.jvxb.manage.livable.service.SysDictService;
import com.jvxb.common.web.RespMsg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author jvxb
 * @since 2021-01-14
 */
@RestController
@RequestMapping("/sysdict/v1")
public class SysdictController {

    @Autowired
    private SysDictService sysDictService;

    @GetMapping("/getSysDictByGroup")
    public RespMsg getSysDictByGroup(String groupName) {
        try {
            List<SysDict> result = sysDictService.getSysDictByGroup(groupName);
            return RespMsg.ok(result);
        } catch (Exception e) {
            return RespMsg.error(e.getMessage());
        }
    }

    @GetMapping("/getUniConfigValue")
    public RespMsg getUniConfigValue(String groupName, String dkey, String dvalue) {
        try {
            String result = sysDictService.getUniValueByGroupAndKey(groupName, dkey, dvalue);
            return RespMsg.ok(result);
        } catch (Exception e) {
            return RespMsg.error(e.getMessage());
        }
    }

    @GetMapping("/updateUniConfigValue")
    public RespMsg update(String groupName, String dkey, String dvalue) {
        try {
            sysDictService.updateConfigValue(groupName, dkey, dvalue);
            return RespMsg.ok();
        } catch (Exception e) {
            return RespMsg.error(e.getMessage());
        }
    }

    @GetMapping("/insertConfigValue")
    public RespMsg insertConfigValue(String groupName, String dkey, String dvalue) {
        try {
            sysDictService.insertConfigValue(groupName, dkey, dvalue);
            return RespMsg.ok();
        } catch (Exception e) {
            return RespMsg.error(e.getMessage());
        }
    }

}
